/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.BankDAO;
import com.ivanbiz.dao.GLAccountDAO;
import com.ivanbiz.dao.GroupAccDAO;
import com.ivanbiz.dao.impl.BankDAOImpl;
import com.ivanbiz.dao.impl.GLAccountDAOImpl;
import com.ivanbiz.dao.impl.GroupAccDAOImpl;
import com.ivanbiz.model.Bank;
import com.ivanbiz.model.GLAccount;
import com.ivanbiz.model.GroupAcc;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author iw20149x
 */
public class AccountActionService extends SelectorComposer<Component> {

    GLAccount gLAccount;
    GroupAccDAO groupAccDAO;
    BankDAO bankDAO;
    GLAccountDAO gLAccountDAO;
    @Wire
    Combobox listBank;
    @Wire
    Combobox listJenisAccount;
    @Wire
    Textbox kode;
    @Wire
    Textbox alamat;
    @Wire
    Textbox noGL;
    @Wire
    Textbox namaGL;
    @Wire
    Window win;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        gLAccount = (GLAccount) Executions.getCurrent().getArg().get("account");
        gLAccountDAO = new GLAccountDAOImpl();
        groupAccDAO = new GroupAccDAOImpl();
        loadJenisAccount();
        bankDAO = new BankDAOImpl();
        loadBank();
    }

    private void loadJenisAccount() {
        try {
            List<GroupAcc> loadJenisAccount = groupAccDAO.getAll(GroupAcc.class);
            for (GroupAcc groupAcc : loadJenisAccount) {
                Comboitem item = new Comboitem();
                item.setValue(groupAcc);
                item.setLabel(groupAcc.getNamaGroup());
                listJenisAccount.appendChild(item);
            }
        } catch (Exception ex) {
            Logger.getLogger(AccountActionService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void loadBank() {
        try {
            List<Bank> loadBank = bankDAO.getAll(Bank.class);
            for (Bank bank : loadBank) {
                Comboitem item = new Comboitem();
                item.setValue(bank);
                item.setLabel(bank.getNama());
                listBank.appendChild(item);
            }
        } catch (Exception ex) {
            Logger.getLogger(AccountActionService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onChange=#listJenisAccount;")
    public void dolistJenisAccount() throws Exception {
        if (listJenisAccount.getValue().equals("Kreditur")) {
            kode.setValue(getGenereateKode());
        } else {
            kode.setValue("XXX");
        }
    }

    private String getGenereateKode() throws Exception {
        String kodes = gLAccountDAO.getLastNoTransaksi();
        if (!"".equals(kodes)) {
            int count = Integer.parseInt(kodes);
            count += 1;
            if (count >= 890) {
                Messagebox.show("Kode lebih dari 3 digit");
            } else {
                kodes = String.format("%03d", count);
            }
        } else {
            kodes = String.format("%03d", 1);
        }
        return kodes;
    }

    @Listen("onClick=#simpan;")
    public void simpan() {
        if (gLAccount == null) {
            gLAccount = new GLAccount();
        }
        gLAccount.setGroupACC(listJenisAccount.getValue());
        gLAccount.setKode(kode.getValue());
        gLAccount.setNama(listBank.getValue());
        gLAccount.setNoGL(noGL.getValue());
        gLAccount.setNameGL(namaGL.getValue());
        gLAccount.setDeskripsi(alamat.getValue());
        validateGLAccount(gLAccount);
    }

    private void validateGLAccount(GLAccount gLAccount) {
        try {
            if (gLAccountDAO.validateField(GLAccount.class, "noGL", gLAccount.getNoGL()).equals("Data sudah ada")) {
                Messagebox.show("No GL sudah ada");
            } else {
                if (gLAccount.getGroupACC().equals("Kreditur")) {
                    gLAccountDAO.saveGLKrediturWithSetting(gLAccount);
                } else if (gLAccount.getGroupACC().equals("BiayaLain")) {
                    gLAccountDAO.saveGLBiayaLainWithSetting(gLAccount);
                } else {
                    gLAccountDAO.saveOrUpdate(gLAccount);
                }
                win.detach();
            }
        } catch (Exception ex) {
            Logger.getLogger(AccountActionService.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
